﻿using System.Collections.Generic;
using System.Linq;
using System.Web.Mvc;
using DotNetDream.Extenstion;
using DotNetDream.Helper;
using DotNetDream.Models;

namespace DotNetDream.Controllers
{
    public class BlogController : DndController
    {
        #region Fields
        public const int PAGE_SIZE = 3;
        #endregion
        /// <summary>
        /// View selected Blog
        /// </summary>
        /// <param name="blogId"></param>
        /// <returns></returns>
        public ActionResult ViewBlog(string id, string year, string month, string page)
        {
            return getBlogView(id, year, month, page, false);
        }

        #region Helper

        #region Blog helper
        /// <summary>
        /// Get a Blog view with its model
        /// </summary>
        /// <param name="blogId"></param>
        /// <param name="yearFilter">Filter by year archive</param>
        /// <param name="monthFilter">Filter by month archive</param>
        /// <param name="pageFilter">Current view page</param>
        /// <param name="modelIsOptional">True to not return view with the model</param>
        /// <returns></returns>
        private ViewResult getBlogView(string blogId, string yearFilter, string monthFilter, string pageFilter, bool modelIsOptional)
        {
            //Model is not needed
            if (modelIsOptional)
            {
                return View();
            }
            else
            {
                //Validated blog Id
                int id = 0;
                if (!int.TryParse(blogId, out id) || id == 0)
                {
                    //TODO: Exception model
                    ViewBag.ErrorMessage = ExceptionMessage.BlogNotFound;
                    return View("Error");
                }

                //Find the blog
                Blog model = Database.Blogs.Find(id);

                //Validate search filters
                int year = 0;
                int month = 0;
                int page = 0;
                int.TryParse(yearFilter, out year);
                int.TryParse(monthFilter, out month);
                int.TryParse(pageFilter, out page);

                //Default as first page
                if (page == 0) page = 1;

                //Populate blog filter values
                model.Year = year;
                model.Month = month;
                model.CurrentPage = page;

                //Both month & year
                if (month > 0 && year > 0)
                {
                    model.BlogEntries = Database.BlogEntries
                        .Where(e => e.Created.Year == year && e.Created.Month == month && e.BlogId == id)
                        .OrderByDescending(e => e.Created)
                        .Skip((page - 1) * PAGE_SIZE)
                        .Take(PAGE_SIZE)
                        .ToList();
                }
                //Year only
                else if (year > 0)
                {
                    model.BlogEntries = Database.BlogEntries
                       .Where(e => e.Created.Year == year && e.BlogId == id)
                       .OrderByDescending(e => e.Created)
                       .Skip((page - 1) * PAGE_SIZE)
                       .Take(PAGE_SIZE)
                       .ToList();
                }
                //Month only - cannot filter
                else if (month > 0)
                {
                    //TODO: Exception model
                    ViewBag.ErrorMessage = ExceptionMessage.BlogNotFound;
                    return View("Error");
                }
                //No filter
                else
                {
                    model.BlogEntries = Database.BlogEntries
                        .Where(e => e.BlogId == id)
                        .OrderByDescending(e => e.Created)
                        .Skip((page - 1) * PAGE_SIZE)
                        .Take(PAGE_SIZE)
                        .ToList();
                }

                //Final check
                if (model == null)
                {
                    //TODO: Exception model
                    ViewBag.ErrorMessage = ExceptionMessage.BlogNotFound;
                    return View("Error");
                }
                else
                {
                    return View(model);
                }
            }
            /*Blog blog = getBlog(blogId);
            if (blog == null)
            {
                //TODO: Exception model
                ViewBag.ErrorMessage = ExceptionMessage.BlogNotFound;
                return View("Error");
            }
            else
            {
                if (modelIsOptional)
                    return View();
                else
                {
                    #region Process blog archive years
                    //blog.BuildArchives();
                    #endregion

                    #region Process year & month filter
                    //Year filter
                    if (!string.IsNullOrEmpty(yearFilter))
                    {
                        //Month filter
                        if (!string.IsNullOrEmpty(monthFilter))
                            blog.BlogEntries =
                                blog.BlogEntries
                                    .Where(entry => entry.Created.Year == int.Parse(yearFilter) && entry.Created.Month == int.Parse(monthFilter)).ToList();
                        else
                            blog.BlogEntries =
                                blog.BlogEntries
                                    .Where(entry => entry.Created.Year == int.Parse(yearFilter)).ToList();
                    }
                    #endregion

                    //Finally sort the entries and return the model
                    blog.BlogEntries = blog.BlogEntries.OrderByDescending(entry => entry.Created).ToList();

                    return View(blog);
                }
            }*/
        }
        #endregion
        #endregion
    }
}